*' --------------
* sameness effects: nonlinearity ----
*' ==============
clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}

* predicted margins for interaction
cap: program drop margin_est
program margin_est
	args X k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post

	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_est_`mname'_`X'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_est_`mname'_`X'_`year'_`geo_type'.txt") replace

end 

cap: program drop margin_interact
program margin_interact 
	args X Y k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin `Y' if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post

	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace

end 

local vv "v2"
local geo_type county

use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 

local year "all"

svyset [pw=weight] 	

if ("`vv'" == "v1"){
	keep if sample_v1 == 1	
}
if ("`vv'" == "v2") {
	keep if sample_v2 == 1	
}

local demographics_individual Female agegroup Race6 BornUSA 
local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local contextual_control pop_density
local post_control_context RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5 RAT_PhysProb RAT_veteran

egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
recode n_miss (0=0) (1/max=1)

drop if n_miss > 0

*--------------
* Figure 3. interaction effects : Table S6, Model 1 to 4
*==============
local eq_demographics_individual Female i.agegroup i.Race6 BornUSA 
local eq_demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local eq_contextual_control pop_density
local eq_post_control RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5 RAT_PhysProb RAT_veteran
local eq_interact_context b2.empstat#c.pop_density b2.empstat#c.RAT_Female b2.empstat#c.RAT_AgeGrp4_2 b2.empstat#c.RAT_AgeGrp4_3 b2.empstat#c.RAT_AgeGrp4_4 b2.empstat#c.RAT_Race6_2 b2.empstat#c.RAT_Race6_3 b2.empstat#c.RAT_Race6_4 b2.empstat#c.RAT_Race6_5 b2.empstat#c.RAT_Race6_6 b2.empstat#c.RAT_BornUSA b2.empstat#c.pop_density b2.empstat#c.RAT_EducGroup_1 b2.empstat#c.RAT_EducGroup_2 b2.empstat#c.RAT_EducGroup_3 b2.empstat#c.RAT_EducGroup_4 b2.empstat#c.RAT_EducGroup_5 b2.empstat#c.RAT_PhysProb b2.empstat#c.RAT_veteran

local model_eq `eq_demographics_individual' `eq_demographics_county' `eq_contextual_control' 

* create intervals 
egen unemp_cat20 = cut(RAT_UnEmpl), group(20) label
tab unemp_cat20

egen emp_cat20 = cut(RAT_Empl), group(20) label
tab emp_cat20

egen nlf_cat20 = cut(RAT_NotInLabor), group(20) label
tab nlf_cat20

estimates clear 

logit Suic i.St `model_eq' b0.unemp_cat20 [pw=weight], cluster(`geo_type'), if empstat == 2
margins unemp_cat20, post
estimates store mall

esttab m* using "./output/AME_model_nonlinear_`geo_type'.csv", csv replace label nogap ci nostar mtitle wide

* redo the same analysis for each year
local vv "v1"

forvalues yy = 2005/2017 {

	use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 
	
	local year "all"
	
	svyset [pw=weight] 	
	
	if ("`vv'" == "v1"){
		keep if sample_v1 == 1	
	}
	if ("`vv'" == "v2") {
		keep if sample_v2 == 1	
	}
	
	
	egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
	recode n_miss (0=0) (1/max=1)
	
	egen unemp_cat20 = cut(RAT_UnEmpl), group(20) label
	tab unemp_cat20
	
	drop if n_miss > 0

	estimates clear
	logit Suic i.St `model_eq' b0.unemp_cat20 [pw=weight], cluster(`geo_type'), if empstat == 2 & Year == `yy'
	margins unemp_cat20, post

	estimates store m`yy'
	esttab m* using "./output/AME_model_nonlinear_`geo_type'_`yy'.csv", csv replace label nogap ci nostar mtitle wide

}
